
class HebPlayerPort {

  //网页生命周期内，只需要初始化一次。
  static init(cfg) {
    if (HebPlayerPort.isNull(cfg)) {
      return -100;
    }
    return HebPlayerPort._initInner(cfg.jsPath, cfg.apiPath, cfg.jsNoCache, cfg.enableLog, cfg.delayInit, cfg.appInfo, cfg.callback);
  }

  static _initInner(videoJsFileUrl, streamMediaApiUrl, noCache, enableLog, delayInit, appInfo, callback) {
    if (false === HebPlayerPort.isNull(HebPlayerPort.corePlayer)) {
      return 0;
    }
    if (HebPlayerPort.getLoadStatus() > 0) {
      return 0;
    }

    if (true === enableLog) {
      HebPlayerPort.enableLog = true;
    } else if (false === enableLog) {
      HebPlayerPort.enableLog = false;
    } else {
      HebPlayerPort.enableLog = null;
    }

    if (window.Number.isInteger(delayInit)) { //秒
      HebPlayerPort.delayInit = delayInit;
    }else {
      HebPlayerPort.delayInit = 0;
    }
    if (HebPlayerPort.delayInit > 120) {
      HebPlayerPort.delayInit = 120;
    }
    if (HebPlayerPort.delayInit <= 0) {
      HebPlayerPort.delayInit = 0;
    }

    let encoding = document.characterSet;
    if ('utf-8' !== encoding && 'UTF-8' !== encoding) {
      return -5;
    }
    if (typeof appInfo !== 'string') {
      appInfo = '';
    }
    HebPlayerPort.appInfo = appInfo

    HebPlayerPort.callback = callback;

    if (!HebPlayerPort.isNull(window.hebVideo)){
      let temp = HebPlayerPort.getScriptFileUrl();
      if (0 !== HebPlayerPort.getApiUrlByJsPath(temp, streamMediaApiUrl)) {
        return -1;
      }
      HebPlayerPort.on_load();
      return 0;
    }

    if (HebPlayerPort.isNull(videoJsFileUrl) || '' === videoJsFileUrl) {
      videoJsFileUrl = 'http://172.21.4.114:9004/player/hebVideo.safe.js';
    } else {
      if (videoJsFileUrl.length < 6) {
        return -2;
      }
      if ('/' === videoJsFileUrl[0] && '/' !== videoJsFileUrl[1]) {
        let curWwwPath = window.document.location.href;
        let pathName = window.document.location.pathname;
        let pos = curWwwPath.lastIndexOf(pathName);
        videoJsFileUrl = curWwwPath.substring(0,pos) + videoJsFileUrl;
      }
    }
    let prefix = videoJsFileUrl.substr(0, 5);
    if (!('http:' === prefix || 'https' === prefix)) {
      return -3;
    }

    if (0 !== HebPlayerPort.getApiUrlByJsPath(videoJsFileUrl, streamMediaApiUrl)){
      return -4;
    }
    //alert(videoJsFileUrl);

    let suffix = '';
    if (true === noCache || HebPlayerPort.isNull(noCache)) { //default, do not cache js file in webbrowser.
      suffix = '?_r=' + Math.random();
    }

    HebPlayerPort.setLoadStatus(1);

    let script = document.createElement('script');
    script.charset = 'UTF-8';
    script.onerror = HebPlayerPort.on_error;
    script.onload = HebPlayerPort.on_load;
    script.src = videoJsFileUrl + suffix;
    document.head.appendChild(script);
    HebPlayerPort.script = script;
  
    return 0;
  }

  static on_error() {
    document.head.removeChild(HebPlayerPort.script);
    HebPlayerPort.script = null;

    HebPlayerPort.setLoadStatus(0);
    console.error('>>>>>>>>>>> fail to load hebVideo');
    //alert('>>>>>>>>>>> fail to load hebVideo');
  }

  static on_load() {
    HebPlayerPort.setLoadStatus(2);
    window.setTimeout(HebPlayerPort._initCore, HebPlayerPort.delayInit*1000);
  }

  static _initCore() {
    let ret = window.hebVideo.init(HebPlayerPort.streamMediaApiUrl, HebPlayerPort.enableLog, HebPlayerPort.appInfo, HebPlayerPort.playerNotify);
    if (window.hebVideo.isFailStr(ret)) {
      document.head.removeChild(HebPlayerPort.script);
      HebPlayerPort.script = null;

      HebPlayerPort.setLoadStatus(0);
      console.error('>>>>>>>>>>> fail to init hebVideo : ' + ret);
      //alert('fail to init hebVideo : ' + ret);
      return;
    }

    HebPlayerPort.nextDivID = 0;
    HebPlayerPort.corePlayer = window.hebVideo;
    if (HebPlayerPort.isNull(HebPlayerPort.corePlayer)) {
      console.warn('>>>>>>>>>>> warn, initializtion is done, but HebPlayerPort.corePlayer is null');
    }

    HebPlayerPort.setLoadStatus(3);
    //console.log('!!! done to initialize hebVideo !!!');
  }

  static setLogEanble(isTrue) {
    if(!HebPlayerPort.isInited()) {
      return '[fail] hebVideo Not initialized yet!';
    }
    return HebPlayerPort.corePlayer.setLogEanble(isTrue);
  }

  static isLogEnable() {
    if(!HebPlayerPort.isInited()) {
      return false;
    }
    return HebPlayerPort.corePlayer.isLogEnable();
  }

  static isInited() {
    return !HebPlayerPort.isNull(HebPlayerPort.corePlayer);
  }

  static playerNotify(token, taskID, containerID, result, detail) {
    console.log('hebVideo notify: ' + token + ' ' + taskID + ' ' + containerID + ' ' + result + ' ' + detail);
    if (!HebPlayerPort.isNull(HebPlayerPort.callback)) {
      HebPlayerPort.callback(token, taskID, containerID, result, detail);
    }
  }

  static playVideo(gbID, divID, callback) {
    if(!HebPlayerPort.isInited()) {
      return '[fail] hebVideo Not initialized yet!';
    }
    if(!HebPlayerPort.isGbID(gbID)) {
      return "[fail] 非法的gb28181设备编号";
    }

    return HebPlayerPort.corePlayer.play(gbID, divID, callback);
  }

  static playVideo2(gbID, divObj, callback) {
    if(!HebPlayerPort.isInited()) {
      return '[fail] hebVideo Not initialized yet!';
    }

    if (HebPlayerPort.isNull(divObj.id) || divObj.id.length <= 0) {
      divObj.id = 'hebP_' + HebPlayerPort.nextDivID;
      HebPlayerPort.nextDivID++;
    }
    return HebPlayerPort.playVideo(gbID, divObj.id, callback);
  }

  static stopVideo(gbID) {
    if(!HebPlayerPort.isInited()) {
      return '[fail] hebVideo Not initialized yet!';
    }
    if(!HebPlayerPort.isGbID(gbID)) {
      return "[fail] 非法的gb28181设备编号";
    }

    return HebPlayerPort.corePlayer.stop(gbID);
  }

  static setMuted(gbID, isTrue) {
    if(!HebPlayerPort.isInited()) {
      return '[fail] hebVideo Not initialized yet!';
    }
    if(!HebPlayerPort.isGbID(gbID)) {
      return "[fail] 非法的gb28181设备编号";
    }

    return HebPlayerPort.corePlayer.setMuted(gbID, isTrue);
  }

  static setTalkback(gbID, enable) {
    if(!HebPlayerPort.isInited()) {
      return '[fail] hebVideo Not initialized yet!';
    }
    if(!HebPlayerPort.isGbID(gbID)) {
      return "[fail] 非法的gb28181设备编号";
    }

    return HebPlayerPort.corePlayer.setTalkback(gbID, enable);
  }

  static setSmoothMode(gbID, enable) {
    if(!HebPlayerPort.isInited()) {
      return '[fail] hebVideo Not initialized yet!';
    }
    if(!HebPlayerPort.isGbID(gbID)) {
      return "[fail] 非法的gb28181设备编号";
    }

    return HebPlayerPort.corePlayer.setSmoothMode(gbID, enable);
  }

  static setAppInfo(str) {
    if(!HebPlayerPort.isInited()) {
      return '[fail] hebVideo Not initialized yet!';
    }
    let encoding = document.characterSet;
    if ('utf-8' !== encoding && 'UTF-8' !== encoding) {
      return '[fail] web page encoding have to be UTF-8!';
    }
    if (typeof str !== 'string') {
      return '[fail] appInfo have to be string!';
    }
    return HebPlayerPort.corePlayer.setAppInfo(str);
  }

  static isErrorStr(str) {
    if (undefined === str || null === str || typeof str !== 'string') {
        return false;
    }
    if (str.indexOf('[fail]') >= 0) {
        return true;
    }
    return false;
  }

  static getScriptFileUrl() {
    let sc = document.getElementsByTagName("script");
    let idx = 0;
    for (; idx < sc.length; idx++) {
      let s = sc.item(idx);
      if (s.src && s.src.match(/hebVideo\.js$/)) {
        return s.src; 
      }
      if (s.src && s.src.match(/hebVideo\.min\.js$/)) {
        return s.src; 
      }
      if (s.src && s.src.match(/hebVideo\.safe\.js$/)) {
        return s.src; 
      }
    }
    return '';
  }

  static getApiUrlByJsPath(scriptFileUrl, streamMediaApiUrl) {
    if (!HebPlayerPort.isNull(streamMediaApiUrl) && typeof(streamMediaApiUrl) === 'string' && streamMediaApiUrl.length > 1){
      let prefix = streamMediaApiUrl.substr(0, 5);
      if (!('http:' === prefix || 'https' === prefix)) {
        return -1;
      }

      HebPlayerPort.streamMediaApiUrl = streamMediaApiUrl;
      return 0;
    }

    let tmp = scriptFileUrl.substr(8);
    let end = tmp.indexOf('/');
    if (end < 0) {
      return -2;
    }
    HebPlayerPort.streamMediaApiUrl = scriptFileUrl.substr(0,8) + tmp.substr(0, end + 1) + 'api';
    return 0;
  }

  static getLoadStatus() {
    if (HebPlayerPort.isNull(HebPlayerPort.coreLoadStatus)) {
      HebPlayerPort.coreLoadStatus = 0;
    }
    return HebPlayerPort.coreLoadStatus;
  }

  static setLoadStatus(stat) {
    HebPlayerPort.coreLoadStatus = stat;
  }

  static isNull(obj) {
    return null === obj || undefined === obj;
  }

  static isGbID(gbID) {
    if (typeof gbID !== 'string' || gbID.length !== 20) {
        return false;
    }
    for (let i = 0; i < gbID.length; i++) {
        if (gbID[i] < '0' || gbID[i] > '9') {
            return false;
        }
    }
    return true;
  }

}

export default HebPlayerPort;
